﻿namespace MyAdmin.NET.Application;

/// <summary>
/// 36码对应旧码表服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class BaseCodeService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<BaseCode> _baseCodeRep;

    public BaseCodeService(SqlSugarRepository<BaseCode> baseCodeRep)
    {
        _baseCodeRep = baseCodeRep;
    }

    /// <summary>
    /// 分页查询36码对应旧码表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    [DisplayName("分页查询36码对应旧码表")]
    public async Task<SqlSugarPagedList<BaseCodeOutput>> Page(PageBaseCodeInput input)
    {
        input.SearchKey = input.SearchKey?.Trim();
        var query = _baseCodeRep.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(input.SearchKey), u =>
                u.ModelNumber.Contains(input.SearchKey)
                || u.Code69.Contains(input.SearchKey)
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.ModelNumber), u => u.ModelNumber.Contains(input.ModelNumber.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Code69), u => u.Code69.Contains(input.Code69.Trim()))
            .Select<BaseCodeOutput>();
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加36码对应旧码表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [DisplayName("增加36码对应旧码表")]
    public async Task<long> Add(AddBaseCodeInput input)
    {
        var entity = input.Adapt<BaseCode>();
        await _baseCodeRep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除36码对应旧码表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    [DisplayName("删除36码对应旧码表")]
    public async Task Delete(DeleteBaseCodeInput input)
    {
        var entity = await _baseCodeRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        //await _baseCodeRep.FakeDeleteAsync(entity);   //假删除
        await _baseCodeRep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新36码对应旧码表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [DisplayName("更新36码对应旧码表")]
    public async Task Update(UpdateBaseCodeInput input)
    {
        var entity = input.Adapt<BaseCode>();
        await _baseCodeRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取36码对应旧码表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    [DisplayName("获取36码对应旧码表")]
    public async Task<BaseCode> Detail([FromQuery] QueryByIdBaseCodeInput input)
    {
        return await _baseCodeRep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取36码对应旧码表列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "List")]
    [DisplayName("获取36码对应旧码表列表")]
    public async Task<List<BaseCodeOutput>> List([FromQuery] PageBaseCodeInput input)
    {
        return await _baseCodeRep.AsQueryable().Select<BaseCodeOutput>().ToListAsync();
    }
}